<?php 
if(!empty($this->points)):
//print_r($this->currentPointOfUser);die;
?>
<script type="text/javascript">
    var pointList = new Array();
    var index = 0;
    <?php 
        foreach($this->points as $key => $item):
            echo '
                pointList[index++] = '.$item->point.'; 
            ';
        endforeach;
    ?>
	var total = <?php echo count($this->points);?>;  
	var i = 0;
	var remainPoint = <?php echo $this->currentPointOfUser->point; ?>;
	for(i=0;i< total;i++){
		jQuery("#point_value_"+i).bind('keyup', function(){
			sumAll();
		});
	}
	
	function isNumberKey(evt,value,order)
	{
		if(evt.which == 8) return true;
		var cur = jQuery("#point_value_"+order).attr('rel');
		jQuery("#point_value_"+order).bind('keyup', function(){
			if(this.value > parseInt(cur)) jQuery("#point_value_"+order).val(cur);
		});
		if(parseInt(value) >= parseInt(cur)) return false;
		var charCode = (evt.which) ? evt.which : event.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57))
			return false;
		return true;
	}
	
	function sumAll(){
		var to = <?php echo count($this->points);?>; 
		var remainPoint = <?php echo $this->totalPoint; ?>;		
		var i = 0;
		var totalAll = 0;
		for(i=0;i< to;i++){
			var countPoint = jQuery("#point_value_"+i).val();
			if(countPoint == '') countPoint = 0;			
			totalAll += parseInt(countPoint);
		}
		var remainPoint = parseInt(remainPoint) - parseInt(totalAll);	
		jQuery("#remainPoint").html("Points to reduce: <font style='color: red;'>"+remainPoint+"</font>");	
		return remainPoint;
	}
	totalPoint = parseInt(totalPoint);
	if(pointList.length > 0) {
		for(var i=0; i<pointList.length; i++){
			if(totalPoint <= pointList[i]){
				jQuery("#point_value_"+i).val(totalPoint);
				i = pointList.length;
			}
			else if(totalPoint > pointList[i]){
				jQuery("#point_value_"+i).val(pointList[i]);
				totalPoint -= parseInt(pointList[i]);
			}
		}
	}
	jQuery("#submit_transaction").click(function(){ 
		var remainPoint = sumAll();
		if(remainPoint != 0){
			alert("Point of user and ask inappropriate gift.");
			return false;
		}
	   document.adminForm.task.value = "savenewredemption";
	   jQuery("#adminForm").submit(); 
	});
</script>
<?php //echo "<pre>";print_r($this->points); ?>
    <table width="100%" class="adminlist extra-adminlist">
      <thead>
        <tr><td colspan="6" align="right"><div id="remainPoint" style="font-weight: bold;"><?php echo "Points to reduce: <font id='re' style='color: red;'>0</font>";?></div></td></tr>
		<tr class="extra-adminlist-tr">
		  <th  nowrap="nowrap">Date</th>	
		  <th  nowrap="nowrap">Transaction ID</th>
          <th nowrap="nowrap">Outlet</th>
          <th nowrap="nowrap">Type</th>
          <th nowrap="nowrap">Points</th>
          <th nowrap="nowrap">Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php 
            $typePoint = array(
                'Transaction',
                'Registration',
                'Add point by hand',
                'Banner click'
            );
            foreach($this->points as $key => $item):
                if($item->point > 0){
					echo '
						<tr valign="middle" class="row'.($key%2).'">
						  <td style="width:10%">';
						  if(!empty($item->date_receipt)){
							echo date("d-m-Y",strtotime($item->date_receipt));
						  }
						  else {
							echo date("d-m-Y",strtotime($item->created_date));
						  }
						  echo '</td>	
						  <td style="width:10%">'.$item->transaction.'</td>
						  <td style="">';
						  if(!empty($item->outlet)){
							echo $item->outlet;
						  }
						  else echo $item->des;	
						  echo '</td>
						  <td style="width:10%">'.$typePoint[$item->type].'</td>
						  <td style="width:10%">'.$item->point.'</td>
						  <td style="width:12%">
							<input rel="'.$item->point.'" title="Type is number" onkeypress="return isNumberKey(event,this.value,'.$key.')" name="point_values[transaction_id_'.$item->id.']" id="point_value_'.$key.'" type="text" value="" style="margin:0;" />
							<input name="transaction_id_'.$item->id.'" type="hidden" value="'.$item->transaction.'" />
							<input name="transaction_id_'.$item->id.'.outlet" type="hidden" value="'.$item->poutlet.'" />
						  </td>
						</tr>
					';
				}
            endforeach;
        ?>
        
      </tbody>  
    </table>
    <div style="margin: 15px 0; text-align: right; margin-right: 50px;">
        <a class="ui-state-default ui-corner-all special-link" id="submit_transaction" style="cursor: pointer;"><span class="ui-icon ui-icon-newwin"></span>Save</a>      
        <input type="submit" value="" style="width: 0; height: 0; border: none; background: none;" />
    </div>
<?php 
endif; 
exit;
?>
